package com.example.springboot.facet;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.facet.*;
import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import java.io.IOException;
import java.util.List;

/**
 * Created by tangmayao on 2018/4/18.
 */
public class SimpleFacetsExample {
    private final Directory indexDir = new RAMDirectory();
    private final Directory taxoDir = new RAMDirectory();
    private final FacetsConfig config = new FacetsConfig();


    public SimpleFacetsExample() {
        config.setHierarchical("Publish Date", true);
    }

    private void index() throws IOException{

        IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(Version.LUCENE_CURRENT,
                new WhitespaceAnalyzer(Version.LUCENE_CURRENT)));

        DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);

        Document doc = new Document();
        doc.add(new TextField("device", "手机", Field.Store.YES));
        doc.add(new TextField("name", "米1", Field.Store.YES));
        doc.add(new FacetField("brand", "小米"));
        doc.add(new FacetField("network", "移动4G"));
        indexWriter.addDocument(config.build(taxoWriter, doc));


        doc = new Document();
        doc.add(new TextField("device", "手机", Field.Store.YES));
        doc.add(new TextField("name", "米4", Field.Store.YES));
        doc.add(new FacetField("brand", "小米"));
        doc.add(new FacetField("network", "联通4G"));
        indexWriter.addDocument(config.build(taxoWriter, doc));

        doc = new Document();
        doc.add(new TextField("device", "手机", Field.Store.YES));
        doc.add(new TextField("name", "荣耀6", Field.Store.YES));
        doc.add(new FacetField("brand", "华为"));
        doc.add(new FacetField("network", "移动4G"));
        indexWriter.addDocument(config.build(taxoWriter, doc));

        doc = new Document();
        doc.add(new TextField("device", "电视", Field.Store.YES));
        doc.add(new TextField("name", "小米电视2", Field.Store.YES));
        doc.add(new FacetField("brand", "小米"));
        indexWriter.addDocument(config.build(taxoWriter, doc));

        taxoWriter.close();
        indexWriter.close();

    }


    private void facetwithSearch() throws IOException {
        IndexReader indexReader= DirectoryReader.open(indexDir);
        IndexSearcher indexSearcher=new IndexSearcher(indexReader);

        TaxonomyReader taxonomyReader=new DirectoryTaxonomyReader(taxoDir);
        FacetsCollector fc=new FacetsCollector();

        //1.查询手机
        System.out.println("-----手机-----");
        TermQuery termQuery=new TermQuery(new Term("device","手机"));
        FacetsCollector.search(indexSearcher,termQuery,10,fc);

        Facets facets=new FastTaxonomyFacetCounts(taxonomyReader,config,fc);
        List<FacetResult> results=facets.getAllDims(10);

        for(FacetResult tmp:results){
            System.out.println(tmp);

        }

        //2.drill down，品牌选小米
        System.out.println("-----小米手机-----");
        DrillDownQuery drillDownQuery=new DrillDownQuery(config,termQuery);
        drillDownQuery.add("brand","小米");
        FacetsCollector fc1=new FacetsCollector();//要new新collector，否则会累加
        FacetsCollector.search(indexSearcher,drillDownQuery,10,fc1);

        facets=new FastTaxonomyFacetCounts(taxonomyReader,config,fc1);
        results=facets.getAllDims(10);

        for(FacetResult tmp:results){
            System.out.println(tmp);

        }


        //3.drill down，小米移动4G手机
        System.out.println("-----移动4G小米手机-----");
        drillDownQuery.add("network", "移动4G");
        FacetsCollector fc2 = new FacetsCollector();
        FacetsCollector.search(indexSearcher, drillDownQuery, 10, fc2);
        facets = new FastTaxonomyFacetCounts(taxonomyReader, config, fc2);
        results = facets.getAllDims(10);
        for (FacetResult tmp : results) {
            System.out.println(tmp);
        }


        //4.drill sideways，横向浏览
        //如果已经进入了小米手机，但是还想看到其他牌子(华为)的手机数目，就用到了sideways
        System.out.println("-----小米手机drill sideways-----");
        DrillSideways ds = new DrillSideways(indexSearcher, config, taxonomyReader);
        DrillDownQuery drillDownQuery1 = new DrillDownQuery(config, termQuery);
        drillDownQuery1.add("brand", "小米");
        DrillSideways.DrillSidewaysResult result = ds.search(drillDownQuery1, 10);
        results = result.facets.getAllDims(10);
        for (FacetResult tmp : results) {
            System.out.println(tmp);
        }

    }


      public static void  main(String[] args) throws IOException {
          SimpleFacetsExample facetsExample=new SimpleFacetsExample();
          facetsExample.index();
          facetsExample.facetwithSearch();

      }

}